<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Zvit_model extends CI_Model  {

	function _construct()
	{
      parent::_construct();
	}
	
	function get_pt_list($day, $sesNum, $obl, $saId){
		$query = $this->db->query("SELECT 
    				rp.pId, e.eoName
					FROM
					    rassadkapunkt rp,
					    eduobjects e,
					    point p
					where
					    rp.subjId = '$day' and rp.sesNum=$sesNum and rp.pId=p.pId and p.saId='$saId' and p.eoId=e.eoId and left(e.terplace,2)=$obl and p.ptId=1
					order by e.eoName");
		return $query->result_array();
	}
	
	function get_subj_name($subjId){
		$query = $this->db->query("select subjName from subjects where subjId=$subjId");
		if ($query->num_rows()>0){
			$row = $query->row();
			return $row->subjName;
		}
		else{
			Logger::getInstance()->log(get_class($this), "Не найден предмет!");
			return "Не знайдено предмет!";
		}
	}
	
	function save_stat_data($data){
		$query = $this->db->query("select pId from operstat where pId='".$data['pId']."' and saId='".$data['saId']."'
									and subjId=".$data['subjId']." and sesNum=".$data['sesNum']);
		if ($query->num_rows() == 0){
			$this->db->insert('operstat', $data);
		}
		else{
			$this->db->where(array('pId' => $data['pId'], 'subjId' => $data['subjId'], 'sesNum' => $data['sesNum'], 'saId' => $data['saId']));
			$this->db->update('operstat', $data);
		}
	}
	
	function save_prob_stat_data($data, $cncData) {
		$str = '';
		$osId = $this->get_osId($data);
		foreach ($cncData as $key => $value) {
			$tmp[] = $key;
			$str .= "($osId, $key, $data[sesNum], $value),";
		}
		$forDel = implode(',', $tmp);
		$insertStr = substr($str, 0, strlen($str)-1);
		$query = $this->db->query("delete from countnotcame where osId=$osId and subjId in ($forDel)");
		$query = $this->db->query("insert into countnotcame (osId, subjId, sesNum, countNotCame) values $insertStr");
	}

	function get_point_name($pId){
		$query = $this->db->query("select eoName from eduobjects where eoId='$pId'");
		if ($query->num_rows()>0){
			$row = $query->row();
			return $row->eoName;
		}
		else{
			$query = $this->db->query("select e.eoName from eduobjects e, point p
									 where p.pId='$pId' and e.eoId=p.eoId");
			if ($query->num_rows()>0){
				$row = $query->row();
				return $row->eoName;				
			}
			else{
				Logger::getInstance()->log(get_class($this), "Не найден пункт в базе!");
				return "Не знайдено пункт!";				
			}
			
		}
	}
	
	function get_stat_data($data, $selectStr){
		$dateTimeArray = array('delivtime', 'sequrin');
		$timeArray = array('timeopen', 'timego');
		$defaultArray = array('sequrout');
		$query = $this->db->query("select $selectStr from operstat o where pId='".$data['pId']."' and saId='".$data['saId']."'
									and subjId=".$data['subjId']." and sesNum=".$data['sesNum']);
		if ($query->num_rows()>0){
			$row = $query->result_array();
			foreach ($row[0] as $key => $value) {
				if (in_array($key, $dateTimeArray)){
					if (!isset($value)){
						$row[0][$key] = date("d.m.Y G:i");
					}
				}
				if (in_array($key, $timeArray)){
					if (!isset($value)){
						$row[0][$key] = date("G:i");
					}
				}
				if (in_array($key, $defaultArray)){
					if (!isset($value)){
						$row[0][$key] = date("01.01.1901 00:00");
					}
				}
			}
			return $row[0];
		}
	}
	
	private function get_osId ($data) {
		$query = $this->db->query("select osId from operstat where pId='".$data['pId']."' and saId='".$data['saId']."'
									and subjId=".$data['subjId']." and sesNum=".$data['sesNum']);
		if ($query->num_rows() > 0) {
			$row = $query->row();
			return $row->osId;
		}
	}

	function get_prob_stat_data($data, $selectStrOs, $selectStrCnc){
		$dateTimeArray = array('delivtime', 'sequrin');
		$timeArray = array('timeopen', 'timego');
		$defaultArray = array('sequrout');
		$query = $this->db->query("select $selectStrOs from operstat o where pId='".$data['pId']."' and saId='".$data['saId']."'
									and subjId=".$data['subjId']." and sesNum=".$data['sesNum']);
		if ($query->num_rows()>0){
			$row = $query->result_array();
			foreach ($row[0] as $key => $value) {
				if (in_array($key, $dateTimeArray)){
					if (!isset($value)){
						$row[0][$key] = date("d.m.Y G:i");
					}
				}
				if (in_array($key, $timeArray)){
					if (!isset($value)){
						$row[0][$key] = date("G:i");
					}
				}
				if (in_array($key, $defaultArray)){
					if (!isset($value)){
						$row[0][$key] = date("01.01.1901 00:00");
					}
				}
			}
			$osId = $this->get_osId($data);
			$query = $this->db->query("select subjId, countNotCame from countnotcame where osId=$osId and subjId in ($selectStrCnc)");
			if ($query->num_rows() > 0) {
				$rez = $query->result_array();
				foreach ($rez as $val) {
					$row[0]['subj'.$val['subjId']] = $val['countNotCame'];
				}
			} else {
				$tmp = explode(',', $selectStrCnc);
				foreach ($tmp as $val) {
					$row[0]['subj'.$val] = 0;
				}
			}
			return $row[0];
		}
	}
	
	function get_filling_inform($data, $selectStr){
		$query = $this->db->query("select $selectStr from operstat o where subjId = '".$data['subjId']."' and sesNum = ".$data['sesNum']." and saId = '".$data['saId']."' and pId='".$data['pId']."'");
		if ($query->num_rows()>0){
			$row = $query->result_array();
			foreach ($row[0] as $key => $value) {
				switch ($key){
					case 'delivkol':
						$ret['stat1'] = ($value!=0) ? 1 : 0;
						break;
					case 'isready':
						$ret['stat2'] = ($value!=0) ? 1 : 0;
						break;
					case 'sequrin':
						$ret['stat3'] = (isset($value)) ? 1 : 0;
						break;
					case 'kolperson':
						$ret['stat4'] = ($value!=0) ? 1 : 0;
						break;
					case 'kolnotcame':
						$ret['stat5'] = ($value!=0) ? 1 : 0;
						break;
					case 'audend':
						$ret['stat6'] = ($value!=0) ? 1 : 0;
						break;
					case 'kolgo':
						$ret['stat7'] = ($value!=0) ? 1 : 0;
						break;
				}
			}
			return $ret;
		}
		
	}
}
?>